/*******************************************************************************
 * Copyright (c) 2001, 2003 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.wizards;

import com.google.common.collect.Maps;

import net.sourceforge.perforce.ui.internal.ServerDetail;
import net.sourceforge.perforce.ui.internal.ServerDetailComposite;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import java.util.HashMap;
import java.util.Map;


/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
class ConfigurationWizardMainPage extends PerforceWizardPage {

  /** Dialog store id constants */
  private static final String STORE_DONT_VALIDATE_ID =
      "ConfigurationWizardMainPage.STORE_DONT_VALIDATE_ID"; //$NON-NLS-1$

  /** In case the page was launched from a different wizard. */
  private IDialogSettings settings;

  /** Store the connection properties */
  private Map<String, Object> properties = Maps.newHashMap();

  /** Indicates whether the validate on exit button is available or not */
  private boolean showValidate;
  private boolean validate;

  /** Holds the detail composite. */
  private ServerDetailComposite serverDetailComposite;

  /**
   * Constructor for ConfigurationWizardMainPage.
   * 
   * @param pageName the name of the page.
   * @param title the title of the page.
   * @param titleImage image to use.
   */
  ConfigurationWizardMainPage(String pageName, String title, ImageDescriptor titleImage) {
    super(pageName, title, titleImage);
  }

  /**
   * Sets the showValidate flag.
   * 
   * @param showValidate the value for the 'showValidate' attribute.
   */
  public void setShowValidate(boolean showValidate) {
    this.showValidate = showValidate;
  }

  /**
   * Returns the value of the validate attribute.
   * 
   * @return true or false.
   */
  public boolean getValidate() {
    return validate;
  }

  /**
   * Sets the properties.
   * 
   * @param properties the new properties.
   */
  public void setProperties(Map<String, Object> properties) {
    this.properties = properties;
  }

  /**
   * Returns the properties
   * 
   * @return the current properties
   */
  public Map<String, Object> getProperties() {
    return properties;
  }

  /** @see IDialogPage#createControl(Composite) */
  public void createControl(Composite parent) {
    serverDetailComposite = new ServerDetailComposite(parent);

    initializeValues();

    serverDetailComposite.setFocus();
    setControl(serverDetailComposite);
  }

  /**
   * Called upon finishing the wizard.
   * 
   * @param monitor monitor for progress feedback.
   * 
   * @return ture or false.
   */
  public boolean finish(IProgressMonitor monitor) {
    final ServerDetail detail = serverDetailComposite.getServerDetail();

    // Set the result to be the current values
    final Map<String, Object> result = new HashMap<String, Object>();
    result.put("port", detail.getServerAddress() + ':' + detail.getServerPort()); //$NON-NLS-1$
    result.put("client", detail.getClient()); //$NON-NLS-1$
    result.put("host", detail.getHost()); //$NON-NLS-1$
    result.put("user", detail.getUser()); //$NON-NLS-1$
    result.put("password", detail.getPassword()); //$NON-NLS-1$
    result.put("charset", detail.getCharacterSet().toString()); //$NON-NLS-1$
    result.put("backgroundUpdates", new Boolean(detail.getBackgroundUpdates()));
    this.properties = result;

    saveWidgetValues();
    return true;
  }

  /**
   * Returns the dialog settings.
   * 
   * @return the current dialog settings.
   */
  IDialogSettings setDialogSettings() {
    return settings;
  }

  /**
   * Sets the dialog settings.
   * 
   * @param settings the new dialog settings.
   */
  void setDialogSettings(IDialogSettings settings) {
    this.settings = settings;
  }

  /** Initializes states of the controls. */
  private void initializeValues() {
    final ServerDetail serverDetail = new ServerDetail(properties);
    serverDetailComposite.setServerDetail(serverDetail);

    IDialogSettings settings = getDialogSettings();
    if (settings != null) {
      if (showValidate) {
        validate = !settings.getBoolean(STORE_DONT_VALIDATE_ID);
        serverDetailComposite.setValidation(validate);
      }
    }
  }

  /** Method saveWidgetValues. */
  private void saveWidgetValues() {
    IDialogSettings settings = getDialogSettings();
    if (settings != null) {
      if (showValidate) {
        settings.put(STORE_DONT_VALIDATE_ID, !validate);
      }
    }
  }

  /**
   * Call this method to create a Group control
   * 
   * @param parent the control's parent.
   * @param text the text.
   * @return the new control.
   */
  @SuppressWarnings("unused")
  // method (should this be removed?)
  private Group createGroup(Composite parent, String text) {
    Group group = new Group(parent, SWT.NULL);
    group.setText(text);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    // data.widthHint = GROUP_WIDTH;

    group.setLayoutData(data);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    group.setLayout(layout);
    return group;

  }
}
